﻿Imports BusinessLogicLayer
Imports BE = BusinessEntities

Partial Class InsertarCliente
    Inherits System.Web.UI.Page

    Dim errores As String
    Dim mensaje As String
    Public Shared cliente As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then

            'Instanciamos la clase 
            Dim capanegocios As New PaisBL

            Try
                ddlPais.DataSource = capanegocios.ListarPaises
                ddlPais.DataValueField = "COD_PAIS"
                ddlPais.DataTextField = "NOM_PAIS"
                ddlPais.DataBind()


            Catch ex As Exception

                errores = capanegocios.ChapaError
                Response.Redirect("Errores.aspx?id=" & errores)
            Finally
                'Liberamos recursos
                capanegocios = Nothing
            End Try
        End If

    End Sub

    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
        'Instanciamos la clase ClienteBL
        Dim capanegocios As New ClienteBL
        'Instancias la entidad ClienteBE
        Dim objeto As New BE.ClienteBE

        'Llenas las propiedades

        objeto.nombre = txtNombres.Text
        objeto.apellido = txtApellidos.Text
        objeto.email = txtEmail.Text
        objeto.direccion = txtDireccion.Text
        objeto.pais = ddlPais.SelectedItem.Value
        objeto.usuario = txtUsuario.Text
        objeto.password = txtPassword.Text



        Try
            If capanegocios.InsertarCliente(objeto) = True Then

                mensaje = "Se insertó el cliente"
                Dim script As String = "<script language=Javascript>"
                script += "alert('" & mensaje & "');"
                script += "</script>"
                Page.ClientScript.RegisterStartupScript(Me.GetType(), "script", script)


            Else

                mensaje = "No se insertó el cliente"
                Dim script As String = "<script language=Javascript>"
                script += "alert('" & mensaje & "');"
                script += "</script>"
                Page.ClientScript.RegisterStartupScript(Me.GetType(), "script", script)

            End If

        Catch ex As Exception
            errores = capanegocios.ChapaError
            Response.Redirect("Errores.aspx?id=" & errores.ToString)
        Finally
            capanegocios = Nothing
            objeto = Nothing
        End Try

    End Sub
End Class
